package com.jspx.txweb.table;

import com.jspx.json.NotExpose;
import com.jspx.sober.annotation.Column;
import com.jspx.sober.annotation.IDType;
import com.jspx.sober.annotation.Id;
import com.jspx.sober.annotation.Table;
import com.jspx.sober.table.OperateTable;
import com.jspx.utils.ArrayUtil;
import com.jspx.utils.StringUtil;

/**
 * 这个表主要是为了保存角色和栏目的权限对应关系，因为每次要得到权限的逻辑关系要遍历所有会耗费较多资源
 * 在每次树结构发生变化后，将通过算法生成角色的权限对应关系保存在这个表里，查询列表数据的时候直接通过这个表查询
 * 来节约资源
 */
@Table(name = "jspx_tree_role", caption = "浏览权限", cache = true, cacheName = "treeItemCache")
public class TreeRole extends OperateTable {
    @Id(auto = true, type = IDType.serial)
    @Column(caption = "ID", notNull = true)
    private long id = 0;

    //总的常量，默认为 0
    @Column(caption = "角色",  notNull = true)
    private int roleId = 0;

    @Column(caption = "栏目ID", length = 10000, notNull = true)
    private String nodeIds = StringUtil.empty;

    @NotExpose
    @Column(caption = "命名空间", length = 50, notNull = true)
    private String namespace = StringUtil.empty;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public int getRoleId() {
        return roleId;
    }

    public void setRoleId(int roleId) {
        this.roleId = roleId;
    }

    public String getNodeIds() {
        return nodeIds;
    }

    public void joinNodeIds(String nodeIds) {
        String[] lines = ArrayUtil.join(StringUtil.split(this.nodeIds,StringUtil.SEMICOLON),StringUtil.split(nodeIds, StringUtil.SEMICOLON));
        lines = ArrayUtil.remove(lines,"");
        lines = ArrayUtil.remove(lines,"root");
        lines = ArrayUtil.deleteRepeated(lines);
        ArrayUtil.sort(lines);
        this.nodeIds = ArrayUtil.toString(lines,StringUtil.SEMICOLON);
    }


    public boolean isInNodeId(String nodeId) {
        if (StringUtil.isNULL(nodeIds)||("*".equals(nodeIds))) return true;
        return ArrayUtil.inArray(StringUtil.split(nodeIds,StringUtil.SEMICOLON),nodeId,true);
    }

    public void setNodeIds(String nodeIds) {
        this.nodeIds = nodeIds;
    }

    public String getNamespace() {
        return namespace;
    }

    public void setNamespace(String namespace) {
        this.namespace = namespace;
    }
}
